package com.quxiu.system.controller;

import java.util.List;

import com.quxiu.common.security.utils.SecurityUtils;
import com.quxiu.system.domain.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.quxiu.common.log.annotation.Log;
import com.quxiu.common.log.enums.BusinessType;
import com.quxiu.common.security.annotation.RequiresPermissions;
import com.quxiu.system.domain.BpmProcInst;
import com.quxiu.system.service.IBpmProcInstService;
import com.quxiu.common.core.web.controller.BaseController;
import com.quxiu.common.core.web.domain.AjaxResult;
import com.quxiu.common.core.web.page.TableDataInfo;

/**
 * 流程实例Controller
 *
 * @author quxiu
 * @date 2024-06-14
 */
@RestController
@RequestMapping("/bpm/inst")
public class BpmProcInstController extends BaseController {
    @Autowired
    private IBpmProcInstService bpmProcInstService;

    /**
     * 查询我的审批列表
     */
    @RequiresPermissions("bpm:approve")
    @GetMapping("/myApproveList")
    public TableDataInfo myApproveList(BpmApproveSearchParam param) {
        Long userId = SecurityUtils.getUserId();
        param.setUserId(userId);
        startPage();
        List<BpmProcInst> list = bpmProcInstService.myApproveList(param);
        return getDataTable(list);
    }

    /**
     * 获取流程实例详细信息
     */
    @RequiresPermissions("bpm:approve")
    @GetMapping(value = "/{procInstId}")
    public AjaxResult getInfo(@PathVariable("procInstId") Long procInstId) {
        Long userId = SecurityUtils.getUserId();
        return success(bpmProcInstService.getBpmProcInstInfo(procInstId, userId));
    }

    /**
     * 新增流程实例
     */
    @RequiresPermissions("bpm:approve")
    @Log(title = "流程实例", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BpmProcInstVo param) {
        Long userId = SecurityUtils.getUserId();
        param.setOwnerId(userId);
        bpmProcInstService.insertBpmProcInst(param);
        return success();
    }

    /**
     * 修改流程实例
     */
    @RequiresPermissions("bpm:approve")
    @Log(title = "流程实例", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BpmProcInst bpmProcInst) {
        return toAjax(bpmProcInstService.updateBpmProcInst(bpmProcInst));
    }

    /**
     * 删除流程实例
     */
    @RequiresPermissions("bpm:approve")
    @Log(title = "流程实例", businessType = BusinessType.DELETE)
	@DeleteMapping("/{procInstIds}")
    public AjaxResult remove(@PathVariable Long[] procInstIds) {
        return toAjax(bpmProcInstService.deleteBpmProcInstByProcInstIds(procInstIds));
    }

    /**
     * 计算审批节点
     */
    @PostMapping("/calculateNode")
    public AjaxResult calculateNode(@RequestBody BpmProcInstVo param) {
        Long userId = SecurityUtils.getUserId();
        param.setOwnerId(userId);
        List<BpmNodeByApplyVo> list = bpmProcInstService.calculateNodeInst(param);;
        return success(list);
    }

    /**
     * 获取审批记录
     */
    @GetMapping("/getApprovalRecord")
    public AjaxResult getApprovalRecord(Long procInstId) {
        Long userId = SecurityUtils.getUserId();
        List<BpmApprovalRecordVo> list = bpmProcInstService.getApprovalRecord(procInstId);;
        return success(list);
    }

    /**
     * 审批看版
     * 获取当前登录人【我发起的、待处理、已处理、我收到的】的审批记录数量
     */
    @GetMapping("/statistics/getApprovalBoard")
    public AjaxResult getApprovalBoard() {
        Long userId = SecurityUtils.getUserId();
        BpmApprovalBoard board = bpmProcInstService.getApprovalBoard(userId);
        return success(board);
    }

}
